var stationId;
var myLatlng;
$(function () {
	stationId = $.getUrlParam("id");
    
    //自定义表单验证
    $('#editStationForm').validationEngine('attach', {
        validationEventTrigger : 'blur',
        promptPosition: 'topRight',
        autoPositionUpdate: true,	//自动调整提示层的位置
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
            if (valid){
                if (stationId == 'null') {
                	addStationInfo();
                } else {
                	updateStationInfo();
                }
            }
        }
    });
     
    if ('null' != stationId){
        //如果stationId不为null，下面是使用stationId进行回显数据
        $.ajax({
            url: "../station/echoStationInfo.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'stationId': stationId},
            async: false,
            success: function (result) {
                if (result.status == 0) {
                	var data = result.data;
                	$("#editStationForm").formatForm(data);
                } else {
                    layer.msg(result.message, {icon: 2});
                }
            }
        });
    }

});


//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//更新
function updateStationInfo() {
    $.ajax({
        url: "../station/modifyStationInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#editStationForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                //关闭弹出窗之前，刷新列表页面的数据
                parent.$table.bootstrapTable('refresh', {url: '../staion/getStationByPage.htm'});//调用父方法刷新
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            } else {
                layer.msg(result.message, {icon: 2});
            }
        }
    });
}

//增加站点信息
function addStationInfo() {
	console.log($("#editStationForm").serialize());
    $.ajax({
        url: "../station/addToStationInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#editStationForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
            	parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../staion/getStationByPage.htm'});
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
            closeForm();
        }
    });
}
